package com.huyn.baseframework.utils;

import android.content.Context;
import android.content.Intent;

import com.android.volley.cache.ACache;
import com.huyn.baseframework.model.AppShare;
import com.huyn.baseframework.model.MemberModel;
import com.huyn.baseframework.model.ThemeModel;

/**
 * Created by huyaonan on 15/10/24.
 */
public class UserUtil {

    private static MemberModel memberModel;
    public static final String FEED_MEMBER = "FEED_MEMBER";

    public static final String LOGIN_SUCCESS = "LOGIN_SUCCESS";
    public static final String LOGOUT = "LOGOUT";

    private static final String BASIC_SHARE_DETAIL = "BASIC_SHARE_DETAIL";

    private static ThemeModel mBasicData;

    public static boolean isUserLogin() {
        return memberModel != null;
    }

    public static MemberModel getMember() {
        return memberModel;
    }

    public static String getMemberId() {
        if (memberModel == null)
            return "";
        return memberModel.memberId;
    }

    public static String getUserHead() {
        if (memberModel == null)
            return "";
        return memberModel.headPic;
    }

    public static String getUserName() {
        if (memberModel == null)
            return "";
        return memberModel.nickname;
    }

    public static String getMemberEncode() {
        if (memberModel == null)
            return "";
        return memberModel.memberToken;
    }

    public static void saveInstance(Context context) {
        if (memberModel == null)
            return;
        ACache.get(context).put(FEED_MEMBER, memberModel);
    }

    public static void saveInstance(Context context, MemberModel model) {
        if (model == null)
            return;
        memberModel = model;
        ACache.get(context).put(FEED_MEMBER, memberModel);
    }

    public static void restoreIntance(Context context) {
        ACache.CacheFeed feed = ACache.get(context).getAsObject(FEED_MEMBER);
        if (feed != null && feed.object != null && feed.object instanceof MemberModel)
            memberModel = (MemberModel) feed.object;

        restoreBasicShareDetails(context);
    }

    public static void logout(Context context) {
        memberModel = null;
        context.sendBroadcast(new Intent(LOGOUT));
        ACache.get(context).remove(FEED_MEMBER);
    }

    public static void clearData(Context context) {
        memberModel = null;
        ACache.get(context).clear();
    }

    public static void saveBasicShareDetails(Context context, ThemeModel data) {
        mBasicData = data;
        ACache.get(context).put(BASIC_SHARE_DETAIL, data);
    }

    private static void restoreBasicShareDetails(Context context) {
        ACache.CacheFeed feed = ACache.get(context).getAsObject(BASIC_SHARE_DETAIL);
        if (feed != null && feed.object != null && feed.object instanceof ThemeModel)
            mBasicData = (ThemeModel) feed.object;
    }

    public static String getFeedBackUrl() {
        return mBasicData != null ? mBasicData.getFeedBackUrl() : "";
    }

    public static String getAboutMeUrl() {
        return mBasicData != null ? mBasicData.getAboutMeUrl() : "";
    }

    public static AppShare getAppShare() {
        return mBasicData != null ? mBasicData.getAppShare() : null;
    }

}
